package com.siwei.mes.service.system.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.siwei.mes.common.query.PageQuery;
import com.siwei.mes.entity.system.TestProjectInfo;
import com.siwei.mes.enums.TestProjectTypeEnum;
import com.siwei.mes.mapper.system.TestProjectInfoMapper;
import com.siwei.mes.service.system.TestProjectInfoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * 试验项目配置(TestProjectInfo)表服务实现类
 *
 * @author linzi
 * @since 2023-12-21 21:01:52
 */
@Service
public class TestProjectInfoServiceImpl implements TestProjectInfoService {
    @Resource
    private TestProjectInfoMapper testProjectInfoMapper;

    @Override
    public Long create(TestProjectInfo entity) {
        testProjectInfoMapper.insertSelective(entity);
        return entity.getId();
    }

    @Override
    public void update(TestProjectInfo entity) {
        TestProjectInfo testProjectInfo = testProjectInfoMapper.selectByPrimaryKey(entity.getId());
        assertTrue(testProjectInfo == null, "试验项目配置信息不存在");
        testProjectInfoMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public void delete(Long id) {
        TestProjectInfo testProjectInfo = testProjectInfoMapper.selectByPrimaryKey(id);
        assertTrue(testProjectInfo == null, "试验项目配置信息不存在");
        testProjectInfoMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<TestProjectInfo> getPage(PageQuery<TestProjectInfo, TestProjectInfo> query) {
        TestProjectInfo searchParam = query.getParams();
        PageInfo<TestProjectInfo> pageInfo = PageHelper.startPage(query.getPageNum(), query.getPageSize())
                .doSelectPageInfo(() -> testProjectInfoMapper.getList(searchParam));
        for (TestProjectInfo projectInfo : pageInfo.getList()) {
            String testTypeName = TestProjectTypeEnum.selectByCodeName(projectInfo.getTestType());
            projectInfo.setTestTypeName(testTypeName);
        }
        return pageInfo;
    }

    @Override
    public TestProjectInfo loadById(Long id) {
        TestProjectInfo projectInfo = testProjectInfoMapper.loadById(id);
        if (projectInfo != null) {
            String testTypeName = TestProjectTypeEnum.selectByCodeName(projectInfo.getTestType());
            projectInfo.setTestTypeName(testTypeName);
        }
        return projectInfo;
    }

    @Override
    public List<TestProjectInfo> getAll(TestProjectInfo entity) {
        List<TestProjectInfo> list = testProjectInfoMapper.getList(entity);
        for (TestProjectInfo projectInfo : list) {
            String testTypeName = TestProjectTypeEnum.selectByCodeName(projectInfo.getTestType());
            projectInfo.setTestTypeName(testTypeName);
        }
        return list;
    }

    @Override
    public Map<String, TestProjectInfo> getAllTestProjectInfo() {
        Map<String, TestProjectInfo> map = new HashMap<>();
        List<TestProjectInfo> list = this.getAll(new TestProjectInfo());
        for (TestProjectInfo projectInfo : list) {
            String key = projectInfo.getTestType() + "_" + projectInfo.getTestName();
            map.put(key, projectInfo);
        }
        return map;
    }
}
